package com.yuwenchao.superafflatus.service.impl;

import cn.binarywang.tools.generator.ChineseIDCardNumberGenerator;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Month;
import cn.hutool.core.util.IdcardUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuwenchao.superafflatus.listener.ExcelTableDTOImplListener;
import com.yuwenchao.superafflatus.pojo.dto.TableDto;
import com.yuwenchao.superafflatus.pojo.entity.SuperTable;
import com.yuwenchao.superafflatus.mapper.SuperTableMapper;
import com.yuwenchao.superafflatus.service.SuperTableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuwenchao.superafflatus.utils.AutomaticCommon;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jdt.internal.compiler.ast.SuperReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ChaooGG
 * @since 2021-05-31
 */
@Service
@Slf4j
public class SuperTableServiceImpl extends ServiceImpl<SuperTableMapper, SuperTable> implements SuperTableService {


    //随机生成较为真实的数据
    @Transactional
    @Override
    public void batchsavesuperTable() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String nowDateTime =df.format(new Date());
        DateTime date = DateUtil.parse(nowDateTime);
        int year;
        int month;
        int day;
        for (int i = 0; i <10 ; i++) {
            SuperTable superTable = new SuperTable();
            //随机生成18位的身份证
            String generate = ChineseIDCardNumberGenerator.getInstance().generate();
            superTable.setSuperIdCard(generate);
            String yearstr = generate.substring(6, 10);
            String monthstr = generate.substring(10, 12);
            String daystr = generate.substring(12, 14);
            year = Integer.parseInt(yearstr);

            month = Integer.parseInt(monthstr);

            day = Integer.parseInt(daystr);
            log.info("year："+year+"month："+month+"day："+day);
            superTable.setSuperName(AutomaticCommon.getName());
            superTable.setSuperAge(IdcardUtil.getAgeByIdCard(generate, date));
            superTable.setSuperSex(AutomaticCommon.getGender());
            superTable.setSuperMobile(AutomaticCommon.getTel());
            //EmailAddressGenerator.getInstance().generate()  另外一种获取邮箱的方式
            superTable.setSuperEmail(AutomaticCommon.getEmail(8,13));
            superTable.setSuperSalary(new BigDecimal(AutomaticCommon.salary()));
            superTable.setSuperAddress(AutomaticCommon.getaddress());
            //星座
            String zodiac = DateUtil.getZodiac(month-1, day);
            superTable.setConstellation(zodiac);
            //属相
            String chineseZodiac = DateUtil.getChineseZodiac(year);
            superTable.setSuperZodiac(chineseZodiac);


            baseMapper.insert(superTable);
        }


    }

    @Override
    public List<TableDto> listInfoData(QueryWrapper<SuperTable> queryWrapper) {

        List<SuperTable> superTables = baseMapper.selectList(queryWrapper);
         //创建ExcelDictDTO列表，将Dict列表转换成ExcelDictDTO列表
        ArrayList<TableDto> tableDtoArrayList = new ArrayList<>(superTables.size());
        superTables.forEach(SuperTable ->{
            TableDto tableDto = new TableDto();
            BeanUtils.copyProperties(SuperTable,tableDto);
            tableDtoArrayList.add(tableDto);
        });

        return tableDtoArrayList;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void importData(InputStream inputStream) {

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, TableDto.class, new ExcelTableDTOImplListener(baseMapper)).sheet().doRead();


    }
}
